package net.sashag.nadlandroid;

import java.util.List;

import android.app.ProgressDialog;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.apps.analytics.easytracking.TrackedActivity;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.parse.ParseException;

@EActivity(R.layout.messages)
public class MessagesActivity extends TrackedActivity {
	
	private static final int DELETE_MESSAGE_MENU_ID = 1;

	@ViewById protected ListView listMessages;
	@ViewById protected LinearLayout llNoMessages;
	
	private ArrayAdapter<Message> adapter;
	private ProgressDialog progress;

	@AfterViews
	protected void populateMessages() {
		setTitle("Your Messages");
		progress = new ProgressDialog(this);
		progress.setTitle("Loading Messages");
		progress.setMessage("Loading your most up to date messages...");
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setCancelable(false);
		progress.show();
		loadMessagesBackground();
		registerForContextMenu(listMessages);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		Message message = adapter.getItem(info.position);
		menu.setHeaderTitle("From " + message.getSenderName());
		menu.add(Menu.NONE, DELETE_MESSAGE_MENU_ID, Menu.NONE, "Delete Message");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() != DELETE_MESSAGE_MENU_ID) {
			return false;
		}
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		Message message = adapter.getItem(info.position);
		adapter.remove(message);
		adapterUpdated();
		message.delete();
		return true;
	}
	
	protected void adapterUpdated() {
		adapter.notifyDataSetChanged();
		if (adapter.getCount() == 0) {
			llNoMessages.setVisibility(View.VISIBLE);
		}
	}

	@Background
	protected void loadMessagesBackground() {
		List<Message> messages;
		try {
			messages = Message.getMessages(null, true /*with apartment info*/);
		} catch (ParseException e) {
			errorLoadingMessages(e.getMessage());
			return;
		}
		adapter = new MessagesAdapter(this, messages);
		finishLoading();
	}
	
	@UiThread
	protected void errorLoadingMessages(String error) {
		progress.dismiss();
		Toast.makeText(this, "Error loading messages: " + error, Toast.LENGTH_LONG).show();
		Log.e("MessagesActivity", "Error loading messages: " + error);
		finish();
	}

	@UiThread
	protected void finishLoading() {
		listMessages.setAdapter(adapter);
		adapterUpdated();
		progress.dismiss();
	}
	
	@Click(R.id.btnSubmitListing)
	protected void onSubmitListingClicked() {
		new SubmitListingActivity_.IntentBuilder_(this).start();
	}
	
}
